package com.newhero.doc2pdf.convert.common;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@WebFilter(filterName = "globalAuthFilter", urlPatterns = "/*")
public class GlobalAuthFilter implements Filter {

  private HttpServletRequest request;

  @Override
  public void init(FilterConfig filterConfig) throws ServletException {}

  @Override
  public void doFilter(
      ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
      throws IOException, ServletException {
    this.request = (HttpServletRequest) servletRequest;
    String requestURI = request.getRequestURI();
    if (requestURI.endsWith(".newhero")) {
      filterChain.doFilter(servletRequest, servletResponse);
    } else {
      servletResponse.setCharacterEncoding("UTF-8");
      servletResponse.setContentType("application/json; charset=UTF-8");
      servletResponse.getWriter().write("{\"code\":-2,\"msg\":\"没有权限\",\"obj\":null}");
    }
  }

  @Override
  public void destroy() {}
}
